/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

/*
 * Created on 9/02/2005
 *
 */

package au.com.lastweekend.jim.model;

import java.io.File;
import java.util.Collections;
import java.util.List;

import javax.swing.AbstractListModel;
import javax.swing.SwingUtilities;

/**
 * @author grant@lastweekend.com.au
 * @version $Id: AbstractContactSheetListModel.java,v 1.9 2006/04/16 09:37:20 ggardner Exp $
 * @param <T>
 */
public abstract class AbstractContactSheetListModel<T extends ContactSheetListItem> extends AbstractListModel
        implements
            ContactSheetListModel {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private List<T> listItems = Collections.emptyList();
    private List<File> cutFiles = Collections.emptyList();

    /**
     * Should only ever be called from the swing thread
     * 
     * @param listItems
     */
    public void setListItems(final List<T> newListItems) {

        Runnable updateGUI = new Runnable() {

            public void run() {

                int oldSize = getSize();

                listItems = newListItems;

                int newSize = getSize();

                if (oldSize < newSize) {
                    if (oldSize > 0) {
                        fireContentsChanged(this, 0, oldSize - 1);
                    }
                    fireIntervalAdded(this, oldSize, newSize - 1);
                } else {
                    if (oldSize > 0 && oldSize > newSize) {
                        fireIntervalRemoved(this, newSize, oldSize - 1);
                    }
                    if (newSize > 0) {
                        fireContentsChanged(this, 0, newSize - 1);
                    }
                }
            }
        };

        SwingUtilities.invokeLater(updateGUI);

    }

    public void refresh(final ContactSheetListItem item) {

        Runnable updateGUI = new Runnable() {

            public void run() {

                int index = listItems.indexOf(item);
                if (index > 0) {
                    fireContentsChanged(this, index, index);
                }
            }

        };
        SwingUtilities.invokeLater(updateGUI);
    }
    
    public int getSize() {

        return listItems == null ? 0 : listItems.size();
    }

    public Object getElementAt(int index) {

        return listItems.get(index);

    }

    public void setCutItems(List<File> files) {

        if (files == null) {
            files = Collections.emptyList();
        }
        cutFiles = files;

    }

    public boolean isCut(File file) {

        return (cutFiles.contains(file));
    }

    @SuppressWarnings("unchecked")
    public T getListItemAt(int i) {

        return (T) getElementAt(i);
    }

    public List<T> getListItems() {

        return listItems;
    }



}